
//
//  HMLoginVC.m
//  weiboOC
//
//  Created by 刘耀宗 on 16/4/9.
//  Copyright © 2016年 刘耀宗. All rights reserved.
//

#import "HMLoginVC.h"
#import <SVProgressHUD.h>
#import "OAUTH2.h"

@interface HMLoginVC ()<UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webView;

@end

@implementation HMLoginVC
//懒加载
-(UIWebView *)webView
{
    if (_webView == nil) {
        _webView = [[UIWebView alloc] init];
        _webView.delegate=self;
    }
    return _webView;

}
-(void)loadView
{

    self.view=self.webView;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self setBtn];
    NSURL *url=[self getUrl];
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    [SVProgressHUD showWithStatus:@"正在努力的加载"];
    //进度一直旋转除非调用dismiss
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
    
}
-(NSURL *)getUrl
{
    NSString *urlStr=[NSString stringWithFormat:@"%@?client_id=%@&redirect_uri=%@",OauthUrl,client_id,redirect_uri];
    return [NSURL URLWithString:@"https://www.baidu.com"];

}
-(void)setBtn
{
    //创建
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"填充" style:UIBarButtonItemStylePlain target:self action:@selector(full)];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];


}
-(void)full
{
    NSLog(@"填充");
    NSString *js=@"document.getElementById('userId').value = '648731281@qq.com';document.getElementById('passwd').value = '53663168jin'";
    [self.webView stringByEvaluatingJavaScriptFromString:js];
    

}
-(void)cancel
{

    NSLog(@"取消");
    [self dismissViewControllerAnimated:YES completion:nil];
}
//加载完毕
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"加载完毕");
    [SVProgressHUD dismiss];
    //[self dismissViewControllerAnimated:YES completion:nil];
//    [SVProgressHUD showSuccessWithStatus:@"加载完毕"];

}
//加载出错
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{


    NSLog(@"加载出错");
    [SVProgressHUD showErrorWithStatus:@"网络不给力哦"];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
//    [self cancel];
    

}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //获取
    NSString *urlStr=request.URL.absoluteString;
    //如果是回调授权网站
    if (![urlStr hasPrefix:redirect_uri]) {
        return YES;
    }
    else
    {
        NSString *url= request.URL.query;
        NSString *codeing=@"code=";
        if ([url hasPrefix:codeing]) {
            NSLog(@"点击了授权");
            NSString *coding = [url substringFromIndex:5];
            NSLog(@"code=%@",coding);
            //07387b9c87f1321ca803032c6db3dd65
            //0b38f88feb05429b396516b0cf4a1958
            [[OAUTH2  shareOauth2] load:coding andSuccess:^(NSArray *arr) {
                NSLog(@"成功");
            }];
            
        }
        else
        {
            NSLog(@"点击了取消");
            [self cancel];
        
        }
    
        return YES;
    }

    return NO;
}


@end
